package net.shopxx.action.admin;


import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import net.shopxx.entity.Indexfloor;
import net.shopxx.entity.Indexflooradv;
import net.shopxx.service.IndexfloorService;
import net.shopxx.service.IndexflooradvService;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;

import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

/**
 * 后台Action类 - 首页楼层轮播图
 */

@ParentPackage("admin")
public class IndexflooradvAction extends BaseAdminAction {

	private static final long serialVersionUID = -7712786079159509171L;

	private Indexflooradv indexflooradv;
	private String indexfloorId;
	private Indexfloor indexfloor;

	@Resource
	private IndexflooradvService indexflooradvService;
	@Resource
	private IndexfloorService indexfloorService;
	
	private File picUrl;
	private String picUrlFileName;
	
	// 根据indexfloorId获取已启用的商品属性JSON数据
	public String ajaxIndexflooradv() {
		Indexfloor indexfloor = indexfloorService.load(indexfloorId);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(new String[] { "createDate", "modifyDate", "indexfloor" });
		JSONArray jsonArray = JSONArray.fromObject(indexfloor.getEnabledIndexflooradvList(), jsonConfig);
		return ajaxJson(jsonArray.toString());
	}

	// 添加
	public String add() {
		return INPUT;
	}

	// 编辑
	public String edit() {
		indexflooradv = indexflooradvService.load(id);
		return INPUT;
	}

	// 列表
	public String list() {
		if (StringUtils.isNotEmpty(indexfloorId)) {
			indexfloor = indexfloorService.load(indexfloorId);
			DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Indexflooradv.class);
			detachedCriteria.add(Restrictions.eq("indexfloor", indexfloor));
			pager = indexflooradvService.findByPager(pager, detachedCriteria);
		} else {
			pager = indexflooradvService.findByPager(pager);
		}
		return LIST;
	}

	// 删除
	public String delete() {
		indexflooradvService.delete(ids);
		return ajaxJsonSuccessMessage("删除成功！");
	}

	// 保存
	@Validations(
		requiredStrings = {
			@RequiredStringValidator(fieldName = "indexflooradv.indexfloor.id", message = "首页楼层不允许为空!") 
		}, 
		requiredFields = {
			@RequiredFieldValidator(fieldName = "indexflooradv.isEnabled", message = "是否启用不允许为空!"),
			@RequiredFieldValidator(fieldName = "indexflooradv.orderList", message = "排序不允许为空!") 
		}, 
		intRangeFields = {
			@IntRangeFieldValidator(fieldName = "indexflooradv.orderList", min = "0", message = "排序必须为零或正整数!") 
		}
	)
	@InputConfig(resultName = "error")
	public String save() throws Exception{
		if (picUrl != null){
			String savePath = validateFile(picUrl,picUrlFileName);
			if(ERROR.equals(savePath))
			{
				return ERROR;
			}
			 
			indexflooradv.setPicUrl(savePath);
		}
		
		indexflooradvService.save(indexflooradv);
		if (StringUtils.isNotEmpty(indexfloorId)) {
			redirectionUrl = "indexflooradv!list.action?indexfloorId=" + indexflooradv.getIndexfloor().getId();
		} else {
			redirectionUrl = "indexflooradv!list.action";
		}
		return SUCCESS;
	}

	// 更新
	@Validations(
        requiredStrings = {
            @RequiredStringValidator(fieldName = "indexflooradv.name", message = "楼层adv名称不允许为空!"), 
            @RequiredStringValidator(fieldName = "indexflooradv.indexfloor.id", message = "首页楼层不允许为空!") 
        }, 
        requiredFields = {
            @RequiredFieldValidator(fieldName = "indexflooradv.isEnabled", message = "是否启用不允许为空!"),
            @RequiredFieldValidator(fieldName = "indexflooradv.orderList", message = "排序不允许为空!") 
        }, 
        intRangeFields = {
            @IntRangeFieldValidator(fieldName = "indexflooradv.orderList", min = "0", message = "排序必须为零或正整数!") 
        }
    )
	@InputConfig(resultName = "error")
	public String update() {
		Indexflooradv persistent = indexflooradvService.load(id);
		
		BeanUtils.copyProperties(indexflooradv, persistent, new String[] {"id", "createDate", "modifyDate"});
		indexflooradvService.update(persistent);
		if (StringUtils.isNotEmpty(indexfloorId)) {
			redirectionUrl = "indexflooradv!list.action?indexfloorId=" + indexflooradv.getIndexfloor().getId();
		} else {
			redirectionUrl = "indexflooradv!list.action";
		}
		return SUCCESS;
	}

    public Indexflooradv getIndexflooradv() {
        return indexflooradv;
    }

    public void setIndexflooradv(Indexflooradv indexflooradv) {
        this.indexflooradv = indexflooradv;
    }

    public String getIndexfloorId() {
        return indexfloorId;
    }

    public void setIndexfloorId(String indexfloorId) {
        this.indexfloorId = indexfloorId;
    }

    public Indexfloor getIndexfloor() {
        return indexfloor;
    }

    public void setIndexfloor(Indexfloor indexfloor) {
        this.indexfloor = indexfloor;
    }

 // 获取所有首页楼层
    public List<Indexfloor> getAllIndexfloor() {
        return indexfloorService.getAll();
    }

	public File getPicUrl() {
		return picUrl;
	}

	public void setPicUrl(File picUrl) {
		this.picUrl = picUrl;
	}

	public String getPicUrlFileName() {
		return picUrlFileName;
	}

	public void setPicUrlFileName(String picUrlFileName) {
		this.picUrlFileName = picUrlFileName;
	}
	

}